package src;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFileChooser;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * UserName.java
 *
 * Created on Dec 8, 2011, 5:25:49 PM
 */
/**
 * 
 * @author Ahmed
 */
public class UserName extends javax.swing.JFrame {

	/** Creates new form UserName */
	public UserName() {
		initComponents();
	}

	private String url;
	private String nameStat;
	private String passwordStat;

	public String getNameStat() {
		return nameStat;
	}

	public String getPasswordStat() {
		return passwordStat;
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed"
	// desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		jDesktopPane1 = new javax.swing.JDesktopPane();
		submit = new javax.swing.JToggleButton();
		jSeparator1 = new javax.swing.JSeparator();
		userName = new javax.swing.JTextField();
		jLabel1 = new javax.swing.JLabel();
		password = new javax.swing.JPasswordField();
		jLabel2 = new javax.swing.JLabel();
		createSchema = new javax.swing.JToggleButton();
		loadSchema = new javax.swing.JToggleButton();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		setTitle("User Info");
		setResizable(false);

		submit.setFont(new java.awt.Font("Tahoma", 1, 14));
		submit.setForeground(new java.awt.Color(0, 0, 153));
		submit.setText("Submit");
		submit.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				submitActionPerformed(evt);
			}
		});
		submit.setBounds(150, 140, 91, 25);
		jDesktopPane1.add(submit, javax.swing.JLayeredPane.DEFAULT_LAYER);
		jSeparator1.setBounds(0, 180, 265, 10);
		jDesktopPane1.add(jSeparator1, javax.swing.JLayeredPane.DEFAULT_LAYER);
		userName.setBounds(40, 40, 200, 30);
		jDesktopPane1.add(userName, javax.swing.JLayeredPane.DEFAULT_LAYER);

		jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11));
		jLabel1.setForeground(new java.awt.Color(255, 255, 255));
		jLabel1.setText("User Name");
		jLabel1.setBounds(40, 20, 61, 14);
		jDesktopPane1.add(jLabel1, javax.swing.JLayeredPane.DEFAULT_LAYER);
		password.setBounds(40, 100, 200, 30);
		jDesktopPane1.add(password, javax.swing.JLayeredPane.DEFAULT_LAYER);

		jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11));
		jLabel2.setForeground(new java.awt.Color(255, 255, 255));
		jLabel2.setText("Password");
		jLabel2.setBounds(40, 80, 54, 14);
		jDesktopPane1.add(jLabel2, javax.swing.JLayeredPane.DEFAULT_LAYER);

		createSchema.setBackground(new java.awt.Color(255, 255, 255));
		createSchema.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
		createSchema.setForeground(new java.awt.Color(102, 0, 0));
		createSchema.setText("Create Schema");
		createSchema.setBounds(10, 200, 120, 40);
		jDesktopPane1.add(createSchema, javax.swing.JLayeredPane.DEFAULT_LAYER);

		loadSchema.setBackground(new java.awt.Color(255, 255, 255));
		loadSchema.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
		loadSchema.setForeground(new java.awt.Color(102, 0, 0));
		loadSchema.setText("Load Schema");
		loadSchema.setBounds(140, 200, 120, 40);
		jDesktopPane1.add(loadSchema, javax.swing.JLayeredPane.DEFAULT_LAYER);

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				layout.createSequentialGroup()
						.addContainerGap()
						.addComponent(jDesktopPane1,
								javax.swing.GroupLayout.DEFAULT_SIZE, 270,
								Short.MAX_VALUE).addContainerGap()));
		layout.setVerticalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				javax.swing.GroupLayout.Alignment.TRAILING,
				layout.createSequentialGroup()
						.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,
								Short.MAX_VALUE)
						.addComponent(jDesktopPane1,
								javax.swing.GroupLayout.PREFERRED_SIZE, 252,
								javax.swing.GroupLayout.PREFERRED_SIZE)
						.addContainerGap()));

		pack();
	}// </editor-fold>//GEN-END:initComponents

	private void submitActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_submitActionPerformed
		// TODO add your handling code here:
		nameStat = userName.getText();
		passwordStat = password.getText();
		if (nameStat.equals("") && passwordStat.equals("")) {
			createSchema.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					JFileChooser chooser = new JFileChooser();
					try {
						int returnVal = chooser.showSaveDialog(chooser);
						if (returnVal == JFileChooser.APPROVE_OPTION) {
							url = chooser.getSelectedFile().getPath();
							int j = 0;
							for (j = url.length() - 1; j >= 0; j--) {
								if (url.charAt(j) == '\\')
									break;
							}
							String schemaURL = url.substring(0, j );
							String schemaName = url.substring(j+1, url.length());
							System.out.println(schemaName+schemaURL);
							Schema schema = new Schema(schemaURL, schemaName);
							schema.applyChanges();
							MainMenu MM = new MainMenu();
							MM.setSchema(schema);
							MM.main(null);
							
							
						}
					} catch (Exception e) {
						System.out.println("exception");
					}
				}
			});
			loadSchema.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					JFileChooser chooser = new JFileChooser();
					try {
						int returnVal = chooser.showOpenDialog(chooser);
						if (returnVal == JFileChooser.APPROVE_OPTION) {
							url = chooser.getSelectedFile().getPath();
							int j = 0;
							for (j = url.length() - 1; j >= 0; j--) {
								if (url.charAt(j) == '\\')
									break;
							}
							String schemaURL = url.substring(0, j);
							String schemaName = url.substring(j+1,url.length() - 4);
							Schema schema = new Schema(schemaURL, schemaName,
									true);
							schema.applyChanges();
							MainMenu MM = new MainMenu();
							MM.setSchema(schema);
							MM.main(null);
							
							
						}
					} catch (Exception e) {
						// TODO: handle exception
					}
				}
			});
		}
	}// GEN-LAST:event_submitActionPerformed

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[]) {
		/* Set the Nimbus look and feel */
		// <editor-fold defaultstate="collapsed"
		// desc=" Look and feel setting code (optional) ">
		/*
		 * If Nimbus (introduced in Java SE 6) is not available, stay with the
		 * default look and feel. For details see
		 * http://download.oracle.com/javase
		 * /tutorial/uiswing/lookandfeel/plaf.html
		 */
		try {
			for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager
					.getInstalledLookAndFeels()) {
				if ("Nimbus".equals(info.getName())) {
					javax.swing.UIManager.setLookAndFeel(info.getClassName());
					break;
				}
			}
		} catch (ClassNotFoundException ex) {
			java.util.logging.Logger.getLogger(UserName.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		} catch (InstantiationException ex) {
			java.util.logging.Logger.getLogger(UserName.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		} catch (IllegalAccessException ex) {
			java.util.logging.Logger.getLogger(UserName.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		} catch (javax.swing.UnsupportedLookAndFeelException ex) {
			java.util.logging.Logger.getLogger(UserName.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		}
		// </editor-fold>

		/* Create and display the form */
		java.awt.EventQueue.invokeLater(new Runnable() {

			public void run() {
				System.out.println("sssss");
				new UserName().setVisible(true);
			}
		});
	}

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JToggleButton createSchema;
	private javax.swing.JDesktopPane jDesktopPane1;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JSeparator jSeparator1;
	private javax.swing.JToggleButton loadSchema;
	private javax.swing.JPasswordField password;
	private javax.swing.JToggleButton submit;
	private javax.swing.JTextField userName;
	// End of variables declaration//GEN-END:variables
}
